package br.ufrgs.inf.engsoft.stp.view.exchange;

import java.util.List;

import br.ufrgs.inf.engsoft.stp.exchange.ExchangeContainer;
import br.ufrgs.inf.engsoft.stp.exchange.ExchangeObject;

import com.vaadin.ui.Table;

@SuppressWarnings("serial")
public class ExchangeTable extends Table {
	
	public static final String[] COLUMNS_HEADERS = {
		"Produto em vista",
		"Produto oferecido",
		"Estado"
	};
	
	public static final String[] VISIBLE_COLUMNS = {
		ExchangeObject.PROPERTY_P1,
		ExchangeObject.PROPERTY_P2,
		ExchangeObject.PROPERTY_STATUS
	};
	
    public static final String[] SORT_FIELD = { ExchangeObject.PROPERTY_P1 };
    private static final boolean SORT_ENABLE[] = { true };

    public ExchangeTable() {
    	ExchangeContainer container = null;
		try {
			container = new ExchangeContainer();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		container.addNestedContainerProperty(ExchangeObject.PROPERTY_P1);
		container.addNestedContainerProperty(ExchangeObject.PROPERTY_P2);
		setContainerDataSource(container);
        setSelectable(true);
        setImmediate(true);
        setVisibleColumns(VISIBLE_COLUMNS);
        setColumnHeaders(COLUMNS_HEADERS);
    }    

    public void cleanTable() {
        if (size() > 0) {
            removeAllItems();
        }
        select(null);
    }
    
    public void updateTable(List<ExchangeObject> list) {
        cleanTable();
        if(list != null) {
	        for (ExchangeObject e : list) {
                getContainerDataSource().addItem(e);
	        }
	        sortTable();
        }
    }

    public void sortTable() {
        sort(SORT_FIELD, SORT_ENABLE);
    }
}